// Chrometris 1.0 for Chrome OS
// Copyright (c) 2010 Joseph Huckaby

Game.width = 1024;
Game.height = 768;
Game.fps = 60;

Game.images = [
	'images/main_title.png',
	'images/in_game_title.png',
	'images/bkgnd-inner.png',
	'images/bkgnd-outer.png',
	'images/tile.png'
];

Game.audio = [
	'audio/blocked.mp3',
	'audio/drop.mp3',
	'audio/line.mp3',
	'audio/music_level.mp3?loop=1',
	'audio/rotate.mp3',
	'audio/title.mp3',
	'audio/start.mp3',
	'audio/game_over.mp3'
];

var Chrometris = {
	
	pieces: [
		[
			[0,0,0,0],
			[0,1,1,0],
			[0,1,1,0],
			[0,0,0,0]
		],
		[
			[0,1,0,0],
			[0,1,0,0],
			[0,1,0,0],
			[0,1,0,0]
		],
		[
			[0,1,0,0],
			[0,1,0,0],
			[0,1,1,0],
			[0,0,0,0]
		],
		[
			[0,0,1,0],
			[0,0,1,0],
			[0,1,1,0],
			[0,0,0,0]
		],
		[
			[0,0,0,0],
			[0,1,0,0],
			[1,1,1,0],
			[0,0,0,0]
		],
		[
			[0,0,0,0],
			[0,1,1,0],
			[0,0,1,1],
			[0,0,0,0]
		],
		[
			[0,0,0,0],
			[0,1,1,0],
			[1,1,0,0],
			[0,0,0,0]
		]	
	],
	
	colors: ['red', 'green', 'yellow', 'blue', 'ltblue', 'purple', 'orange'],
	
	getRandPieceIdx: function() {
		return Math.floor( Math.random() * this.pieces.length );
	},
	
	reset: function() {
		Port.removeAll();
		delete this.playfield;
		delete this.player;
		
		Port.removeDiv( 'main_title' );
		Port.removeDiv( 'title_options' );
		Port.removeDiv( 'in_game_title' );
		Port.removeDiv( 'in_game_controls' );
		Port.removeDiv( 'next_piece_disp' );
		Port.removeDiv( 'lines_disp' );
		
		this.hideDialog();
		
		Game.resetKeys();
	},
	
	titleScreen: function() {
		// show title screen
		this.reset();
		$A.quiet();
		
		Port.addDiv({ id: 'main_title', x: 290, y: 384 - 220, width: 400, height: 140 }).fadeIn(30);
		
		Port.addDiv({ id: 'title_options', x: 'center', y: 300, width: 480, height: 224,
		 	html: 
				'<div class="title_text">Use the left and right arrow keys to move your piece.  Use the up arrow key to rotate, and the down arrow key to drop.  Try to fill horizontal lines.</div>' + 
				'<div class="title_option"><a href="javascript:void($C.startNewGame())">Start New Game</a></div>' + 
				'<div class="title_option"><a href="javascript:void($C.about())">About</a></div>'
		}).fadeIn(30);
		
		this.mode = 'title';
		
		$A.playSound('title');
		
		if (!Game.inGame) Game.run();
	},
	
	startNewGame: function() {
		$A.playSound('start');
		
		// start new game
		this.reset();
		
		// game stats
		this.frameMod = 50;
		this.level = 1;
		this.lines = 0;
		
		Port.addDiv({ id: 'in_game_title', x: 20, y: 650, width: 218, height: 102 }).fadeIn(30);
		Port.addDiv({ id: 'in_game_controls', 
			html: '<a href="javascript:void($C.pause())">Pause</a> &nbsp; <a href="javascript:void($C.titleScreen())">Exit</a>'
		}).fadeIn(30);
		
		Port.addDiv({ id: 'next_piece_disp', x: 720, y: 64 }).fadeIn(30);
		Port.addDiv({ id: 'lines_disp', x: 200, y: 64 }).fadeIn(30);
		
		this.playfield = new Playfield();
		Port.addPlane( this.playfield );
		
		/* var tile = new Tile();
		tile.className = 'tile red';
		this.playfield.addSprite( tile ); */
		
		this.player = new Player();
		this.playfield.addSprite( this.player );
		this.player.setupNewPiece( this.getRandPieceIdx() );
		
		this.nextPiece = this.getRandPieceIdx();
		
		this.mode = 'game';
		
		this.updateDisp();
		
		if (!ua.ipad) {
			$A.rewind('music_level');
			$A.play('music_level');
		}
	},
	
	getPiecePreview: function(idx) {
		var piece = Chrometris.pieces[idx];
		var color = Chrometris.colors[idx];
		
		var html = '<div class="piece_preview">';
		
		for (var y = 0; y < 4; y++) {
			for (var x = 0; x < 4; x++) {
				var ch = piece[x][y];
				html += ch ? ('<div class="tile '+color+'" style="float:left;"></div>') : '<div class="tile_empty_float"></div>';
			}
		}
		
		html += '<div class="clear"></div>';
		html += '</div>';
		return html;
	},
	
	updateDisp: function() {
		// update display divs
		if ($('next_piece_disp')) {
			$('next_piece_disp').innerHTML = this.getPiecePreview(this.nextPiece) + '<div class="disp_caption">Next</div>';
			$('lines_disp').innerHTML = '<div class="disp_large">'+this.lines+'</div><div class="disp_caption">Lines</div>';
		}
	},
	
	pause: function() {
		Game.stop();
		$A.quiet();
		$A.playSound('start');
		$('in_game_controls').innerHTML = '<a href="javascript:void($C.resume())">Resume</a> &nbsp; <a href="javascript:void($C.titleScreen())">Exit</a>';
	},
	
	resume: function() {
		Game.run();
		if (this.mode == 'game') $A.play('music_level');
		$('in_game_controls').innerHTML = '<a href="javascript:void($C.pause())">Pause</a> &nbsp; <a href="javascript:void($C.titleScreen())">Exit</a>';
	},
	
	about: function() {
		// show about screen
		var html = '';
		html += '<div class="about">';
		
		html += '<div class="title">Chrometris 1.0</div>';
		html += '<div>Developed for Chromium OS</div>';
		
		html += '<div class="title">Game Design</div>';
		html += '<div>Min Chang</div>';
		
		html += '<div class="title">Programming</div>';
		html += '<div>Joseph Huckaby</div>';
		
		html += '<br/><br/>';
		
		html += '<div class="dialog_links"><a href="javascript:void($C.hideDialog())">Close</a></div>';
		
		html += '</div>';
		this.showDialog( html );
	},
	
	showDialog: function(html) {
		this.hideDialog();
		Port.addDiv({ id: 'dialog_overlay', x:0, y:0, width:Game.width, height:Game.height });
		Port.addDiv({ id: 'dialog', x:'center', y:'center', width:640, height:320, html:html });
	},
	
	hideDialog: function() {
		Port.removeDiv( 'dialog_overlay' );
		Port.removeDiv( 'dialog' );
	}
	
};

var $C = Chrometris;

Game.addEventListener( 'load', function() {
	// engine is loaded
	Port.div.style.backgroundImage = 'url(images/bkgnd-outer.png)';
	Port.div.style.backgroundRepeat = 'no-repeat';
	
	// simple touch controls
	Port.div.ontouchstart = function(e) {
		if ($G.inGame && ($C.mode == 'game')) {
			var size = getInnerWindowSize();
			var x = e.touches[0].screenX;
			var y = e.touches[0].screenY;
			
			if (y < 200) {
				// rotate
				$C.player.handleKeyDown( Game.UP_ARROW );
			}
			else if (y < size.height - 60) {
				if (x < (size.width / 2)) $C.player.handleKeyDown( Game.LEFT_ARROW );
				else $C.player.handleKeyDown( Game.RIGHT_ARROW );
			}
		}
	};
	
	Chrometris.titleScreen();
	// Chrometris.startNewGame();
} );

Game.addEventListener( 'keydown', function(code, e) {
	if ($C.player) $C.player.handleKeyDown( code, e );
	
	if (code == Game.ESCAPE) {
		if (Game.inGame) $C.pause(); else $C.resume();
	}
	
	if (code == Game.BACKSPACE) {
		$C.nextPiece = 1;
		$C.updateDisp();
	}
} );

